//
//  MBSpaceInFormationViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceInFormationViewController.h"

@interface MBSpaceInFormationViewController ()<UITextFieldDelegate> {
    UIView                          *bgView;
    UITextField                     *_textField;
}

@end

@implementation MBSpaceInFormationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"完善资料"];
    
    [self setBackItem];
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt setTitle:@"完成" forState:UIControlStateNormal];
    [bt setTitle:@"完成" forState:UIControlStateHighlighted];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    bt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [bt addTarget:self action:@selector(onSelectItemForWork) forControlEvents:UIControlEventTouchUpInside];
    [self setRightBarItems:bt];
    
    [self onCreateUI];
}

- (void)onCreateUI
{
    bgView = [[UIView alloc]initWithFrame:CGRectMake(25*PERCEN, 64+20*PERCEN, SCREEN_WIDTH-50*PERCEN, 80*PERCEN)];
    if (ISIPAD) {
        bgView.frame = CGRectMake(25*PERCEN, 64+20*PERCEN, SCREEN_WIDTH-50*PERCEN, 80*PERCEN);
    } else {
        bgView.frame = CGRectMake(25*PERCEN, (ISPortrait?NAV_HEIGHT+20*PERCEN:44+20*PERCEN), SCREEN_WIDTH-50*PERCEN, 80*PERCEN);
    }
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5.0f;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    
    NSString *showTitle = @"请输入您的昵称，10个字符以内";
    if (_isSign) {
        showTitle = @"请输入您的个性签名，50字符以内";
    }
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(20*PERCEN, (80*PERCEN-30)/2., bgView.width-30*PERCEN, 30)];
    _textField.placeholder = showTitle;
    _textField.delegate = self;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.borderStyle = UITextBorderStyleNone;
    _textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:showTitle attributes:@{NSForegroundColorAttributeName: UIColorFromHex(0x999999),NSFontAttributeName:[UIFont systemFontOfSize:13.f]}];
    _textField.font = [UIFont systemFontOfSize:13.f];
    [bgView addSubview:_textField];
    if ([_signTitle isKindOfClass:[NSString class]]) {
        if (_signTitle.length > 0) {
            _textField.text = _signTitle;
        }
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapForHideAction)];
    [self.view addGestureRecognizer:tap];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    if (ISIPAD) {
        bgView.frame = CGRectMake(25*PERCEN, 64+20*PERCEN, SCREEN_WIDTH-50*PERCEN, 80*PERCEN);
    } else {
        bgView.frame = CGRectMake(25*PERCEN, (ISPortrait?NAV_HEIGHT+20*PERCEN:44+20*PERCEN), SCREEN_WIDTH-50*PERCEN, 80*PERCEN);
    }
    _textField.frame = CGRectMake(20*PERCEN, (80*PERCEN-30)/2., bgView.width-40*PERCEN, 30);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Actions
//完成
- (void)onSelectItemForWork
{
    if (_textField.text.length > 0) {
        if (_isSign) {
            UserModel *model = [UserManager standerDefult].userModel;
            if (model) {
                NSString*url=[NSString stringWithFormat:UPDATE_USER_INFO_URL_PATH,model.UserID,model.NickName,model.FaceImg,model.UserStatus,_textField.text,model.BabyBirthday,model.BabySex];
//                url= [url  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
               
                [[HTTPManager standerDefault]AFPostRequest: url view:nil parameters:nil success:^(id json) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                    if ([json[@"ret_code"] integerValue]==0) {
                        
                        UserModel *model = [UserManager standerDefult].userModel;
                        model.Signature = _textField.text;
                        if (self.InformationBlock) {
                            self.InformationBlock(_textField.text);
                            [self back];
                        }
                        
                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                }];
            }
        } else {
            if (self.InformationBlock) {
                self.InformationBlock(_textField.text);
                [self back];
            }
        }
    }
}

- (void)onTapForHideAction
{
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int lengthCount = (int)(textField.text.length + string.length);
    if (lengthCount >= (_isSign?51:11)) {
        return NO;
    }

    return YES;
}


@end
